package com.sims.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sims.bean.Post;
import com.sims.bean.Sector;
import com.sims.bean.Theme;
import com.sims.dao.IPostDAO;
import com.sims.dao.ISectorDAO;
import com.sims.dao.IThemeDAO;
import com.sims.util.Constant;
import com.sims.util.HttpSessionUtil;

public class BBSService {
	@Autowired
	ISectorDAO sectorDAO;
	@Autowired
	IThemeDAO themeDAO;
	@Autowired
	IPostDAO postDAO;
	
	public Map<String, String> findSectorTheme(Sector sector, List<Theme> themeList) {
		Map<String, String> result = new HashMap<String, String>();
		
		List<Theme> tempList = themeDAO.findBySid(sector.getSid());
		
		for (Theme theme : tempList) {
			themeList.add(theme);
		}
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> findMyTheme(List themeList) {
		Map<String, String> result = new HashMap<String, String>();
		
		List<Theme> list = themeDAO.findByAuthorId(HttpSessionUtil.getUserId());
		
		for (Theme theme : list) {
			themeList.add(theme);
		}
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> addTheme(Theme theme) {
		Map<String, String> result = new HashMap<String, String>();
		
		themeDAO.attachDirty(theme);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> addPost(Post post) {
		Map<String, String> result = new HashMap<String, String>();
		
		postDAO.attachDirty(post);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> deletePost(Post post) {
		Map<String, String> result = new HashMap<String, String>();
		
		Post post2 = postDAO.findById(post.getPid());
		
		postDAO.delete(post2);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	public Map<String, String> deleteTheme(Theme theme) {
		Map<String, String> result = new HashMap<String, String>();
		
		Theme theme2 = themeDAO.findById(theme.getThid());
		
		themeDAO.delete(theme2);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
}
